package org.example.lease.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.example.lease.model.entities.FeeKey;
import org.example.lease.model.entities.FeeValue;
import org.example.lease.model.result.Result;
import org.example.lease.service.FeeKeyService;
import org.example.lease.service.FeeValueService;
import org.example.lease.vo.fee.FeeKeyVo;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 宋孟林
 * @description
 * @date: 2025/5/16 10:29
 */
@Tag(name = "房间杂费管理")
@RestController
@Slf4j
@RequestMapping("/admin/fee")
public class FeeController {
    @Resource
    private FeeValueService feeValueService;
    @Resource
    private FeeKeyService feeKeyService;

    @Operation(summary = "保存或更改杂费名称")
    @PostMapping("/key/saveOrUpdate")
    public Result<?> saveOrUpdateFeeKey(@RequestBody FeeKey feeKey){
        feeKeyService.saveOrUpdate(feeKey);
        return Result.ok();
    }

    @Operation(summary = "保存或更改杂费值")
    @PostMapping("/value/saveOrUpdate")
    public Result<?> saveOrUpdateFeeValue(@RequestBody FeeValue feeValue){
        feeValueService.saveOrUpdate(feeValue);
        return Result.ok();
    }

    @Operation(summary = "查询所有杂费名称和杂费值列表")
    @GetMapping("/list")
    public Result<?> feeInfoList(){
        List<FeeKeyVo> feeKeyVos = feeKeyService.feeInfoList();
        return Result.ok(feeKeyVos);
    }

    /**
     * 根据id删除杂费名称时，涉及到两个表的关联值，杂费名称表和杂费值表，这里的业务时删除杂费名的时候删除相关的杂费值
     * @param id id
     * @return result
     */
    @Operation(summary = "根据id删除杂费名称")
    @DeleteMapping("/key/deleteById")
    public Result<?> deleteFeeKeyById(@RequestParam Long id){
        feeKeyService.removeById(id);
        feeValueService.remove(Wrappers.lambdaUpdate(FeeValue.class).eq(FeeValue::getFeeKeyId, id));
        return Result.ok();
    }

    @Operation(summary = "根据id删除杂费值")
    @DeleteMapping("/value/deleteById")
    public Result<?> deleteFeeValueById(@RequestParam Long id){
        feeValueService.removeById(id);
        return Result.ok();
    }

}
